<%--
  Created by IntelliJ IDEA.
  User: Li Chuanwei
  Date: 2023-07-16
  Time: 22:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/fontawesome/css/all.css" media="all">
    <style>

        #login{
            width: 100%;
            height: 100vh;
            margin:0px;
            background: url(${pageContext.request.contextPath}/static/img/login-bg.jpg) no-repeat;
            background-size:100% 100%;
            background-attachment:fixed;

        }

        .login-title{
            display: flex;
            align-items: center;
            width:300px;
        }
        .login-title-text{
            font-size: 30px;
            font-weight:700;
            color: #009CFA;
            align-items: center;
            margin: auto;
        }
        .login-content{
            width:300px;
            position: fixed;
            top: 50%;
            left: 80%;
            transform: translate(-50%,-50%);
            background: white;
            padding: 2%;
        }
        .div-tab-title {
            width: 274px;
            display: flex;
            justify-content: space-between;
        }
        .login-btn{
            width: 100%;
        }
        .tip{
            color: #009CFA;
            text-align: right;
            padding:0 0 15px 0;
        }
        .tip span {
            cursor: pointer;
        }
        .findpassword-form-item{
            display: flex;
            width: 100%;
        }
        .findpassword input {
            width: 100%;
        }
        .findpassword .layui-form-label{
            width:80px;
        }
        .findpassword .code-btn{
            margin-left: 20px;
            background-color: white;
        }
        .findpassword-btn{
            width:150px;
        }
        .findpassword  .foot-form-item{
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .save-tip{
            color: #009CFA;
            font-size: 10px;
        }
        /* 对layui的修改 */
        .login-class  .layui-tab-brief>.layui-tab-title .layui-this{
            color: #009CFA;
        }
        .login-class .layui-tab-brief>.layui-tab-more li.layui-this:after, .layui-tab-brief>.layui-tab-title .layui-this:after{
            border: none;
            border-radius: 0;
            border-bottom: 2px solid #009CFA;
        }
        .login-class .layui-form-item .layui-input-inline{
            width: 94%;
            display: flex;
            height: 38px;
            line-height: 38px;
            padding: 0px 10px;
            background-color: #fff;
        }
        .login-class .layui-form-item .layui-input-inline-code{
            /* width: 94%; */
            display: flex;
            justify-content: space-between;

        }
        .login-class .layui-form-item  .layui-input-inline-code-input{
            display: flex;
            height: 38px;
            line-height: 38px;
            padding: 0px 10px;
            background-color: #fff;
        }
        .login-code-btn{
            background-color: #fff;
        }
        .login-class .layui-tab-content{
            padding: 15px 0 0 0;
        }
    </style>
</head>
<body>
<div id="login" class="login-class">
    <a href="${pageContext.request.contextPath}/userIndex">
        <img style="height: 110px;width: 230px;" src="${IMG_SERVER}/sys/frontend/logo.png"/>
    </a>
    <form class="layui-form" action="" method="post">
        <div class="login-content">
            <div class="login-title">
                <div class="login-title-text">并夕夕平台用户登录</div>
            </div>
            <!-- tabs切换开始 -->
            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                <ul class="layui-tab-title div-tab-title">
                    <li  class="layui-this" >账号密码登录</li>
                    <li>手机登录</li>
                </ul>
                <div class="layui-tab-content" data-index="0">
                    <!-- 账号登录、 -->
                    <div class="layui-tab-item layui-show">

                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-username"></i>
                                </div>
                                <input type="text" name="account" id="account" placeholder="账号"   lay-verify="accountverify" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" id="password"  placeholder="密码" lay-verify="passverify" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <!-- 手机登录 -->
                    <div class="layui-tab-item">

                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-cellphone"></i>
                                </div>
                                <input type="text" name="phone" id="phone"  placeholder="手机号" lay-verify="phoneverify" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-inline-code">
                                <div class="layui-input-wrap">
                                    <div class="layui-input-prefix">
                                        <i class="layui-icon layui-icon-vercode"></i>
                                    </div>
                                    <input type="password" name="code" id="code" style="width:150px;"   placeholder="验证码" autocomplete="off" class="layui-input">
                                </div>
                                <div class="login-code-btn">
                                    <button type="button" class="layui-btn layui-btn-primary" id="getCode"
                                            onclick="sendMessage()">获取验证码
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
            <!-- tabs切换结束 -->
            <div class="tip"> <a href="findpassword.html" class="save-tip">忘记密码</a></div>
            <!-- 登录按钮 -->
            <div class="foot-form-item">
                <button class="layui-btn layui-btn-normal login-btn" lay-submit lay-filter="login">登录</button>
            </div>


        </div>
    </form>
</div>
<script>

    var sms = "";

    function sendMessage() {
        var isCaptcha = false;
        var btn = document.getElementById("getCode");
        var mobileInput = document.getElementById("phone");
        var countdown = 60; // 倒计时时间（单位：秒）

        // 验证手机号是否合法
        var phonePattern = /^1\d{10}$/;
        var phoneNumber = mobileInput.value.trim();
        if (!phonePattern.test(phoneNumber)) {
            layer.msg('手机号格式错误', {icon: 2}, function () {
            });
            return;
        }

        layer.open({
            type: 1, // page 层类型
            area: ['400px', '300px'],
            title: '请完成滑块验证码',
            shade: 0.6, // 遮罩透明度
            shadeClose: true, // 点击遮罩区域，关闭弹层
            anim: 0, // 0-6 的动画形式，-1 不开启
            content: '<div id="captchaContainer"></div>',
            success: function (layero, index) {
                // 弹出层加载成功后，在其中初始化 SliderCaptcha
                var captcha = sliderCaptcha({
                    id: 'captchaContainer',
                    height: 200,
                    setSrc: function () {
                        return 'static/img/Pic' + Math.round(Math.random() * 4) + '.jpg';
                    },
                    onSuccess: function () {
                        isCaptcha = true;
                        layer.closeAll();
                        $.ajax({
                            url: "${pageContext.request.contextPath}/getCode",
                            type: "post",
                            data: {
                                phone: phoneNumber,
                            },
                            success: function (result) {
                                sms = result.message;
                            },
                            error: function () {
                                layer.msg('验证码发送失败', {icon: 2}, function () {
                                });
                                return;
                            }
                        });
                        // 禁用按钮
                        btn.disabled = true;

                        // 更新按钮文本，并开始倒计时
                        btn.innerHTML = countdown + " 秒后获取";
                        var timer = setInterval(function () {
                            countdown--;
                            if (countdown > 0) {
                                btn.innerHTML = countdown + " 秒后获取";
                            } else {
                                clearInterval(timer);
                                btn.innerHTML = "获取验证码";
                                btn.disabled = false;
                            }
                        }, 1000); // 每秒更新一次倒计时

                        // 在定时器结束前禁止再次点击按钮
                        setTimeout(function () {
                            clearInterval(timer);
                            btn.innerHTML = "获取验证码";
                            btn.disabled = false;
                        }, countdown * 1000);
                    }
                });
            }
        });
    }

    layui.use(['form', 'element', 'layer', 'jquery'], function () {
        var element = layui.element;
        var form = layui.form;
        var $ = layui.jquery;

        //一些事件
        element.on('tab(docDemoTabBrief)', function (data) {

            if (data.index == 0) {
                $('#phone').val('')
                $('#code').val('')
                $(".layui-tab-content").attr('data-index', 0)
            } else {
                $('#account').val('')
                $('#password').val('')
                $(".layui-tab-content").attr('data-index', 1)

            }
        });
        form.verify({
            //账号
            accountverify: function (value, item) { //value：表单的值、item：表单的DOM对象
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 0) {
                    if (!value) {
                        return '输入框不能为空';
                    }
                    if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                        return '用户名不能有特殊字符';
                    }
                    if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                        return '用户名首尾不能出现下划线\'_\'';
                    }
                }
            }

            //密码
            , passverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 0) {
                    if (!value) {
                        return '输入框不能为空';
                    }
                    if (!/^\w+$/.test(value)) {
                        return '只能输入数字和字母'
                    }
                    if (!/^[A-Za-z0-9]{6,16}$/.test(value)) {
                        return '密码必须6到16位，且不能出现空格'
                    }
                }
            },
            //手机号
            phoneverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 1) {
                    if (!value) {
                        return '输入框不能为空';
                    }
                    if (!/^1[3-9]\d{9}$/.test(value)) {
                        return '请输入正确的手机号';
                    }
                }
            },
            //验证码
            codeverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 1) {
                    if (!value) {
                        return '输入框不能为空';
                    }
                }
            }
        });

        form.on('submit(login)', function (data) {
            let Typeindex = $(".layui-tab-content").attr('data-index');
            if (Typeindex == 1) {
                if (data.field.code !== sms){
                    layer.msg('验证码输入错误', {icon: 2}, function () {
                    });
                    return false;
                }
            }

            var index = layer.load(0, {
                shade: [0.3, '#fff'],
            });

            var formData = data.field;
            // 发送Ajax请求并处理结果
            $.ajax({
                url: '${pageContext.request.contextPath}/user/loginIn',
                type: 'POST',
                data: formData,
                success: function(res){
                    layer.close(index);//关闭弹窗
                    if(res.code == 0 && Typeindex == 0)
                        layer.msg("账号密码错误");
                    else if(res.code == 0 && Typeindex == 1)
                        layer.msg("手机号对应的账户不存在");
                    else if(res.code == 200)
                        window.location.href = "${pageContext.request.contextPath}/userIndex";
                },
                error: function(err){
                    layer.close(index);//关闭弹窗
                    layer.msg("请求出现异常");
                }
            });
            return false;
        });
    });

</script>
</body>
</html>
